<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ShopUser extends Model
{
    use HasFactory;

    protected $table = 'sp_shop_user_2';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //is_delete是否删除  1是 2否
    const SHOP_USER_IS_DELETE = 1;//是
    const SHOP_USER_NOT_DELETE = 2;//否

    //status状态：1已审核 2未审核
    const SHOP_USER_STATUS_IS_CHECK = 1;//已审核
    const SHOP_USER_STATUS_NOT_CHECK = 2;//未审核

    //endisable是否启用：1 启用  2禁用
    const SHOP_USER_ENABLE = 1;//启用
    const SHOP_USER_DISABLE = 2;//禁用

    //is_city_location是否开启城市定位
    const SHOP_USER_CITY_LOCATION_OPEN = 1;//开启
    const SHOP_USER_CITY_LOCATION_CLOSE = 2;//关闭

    //account融资非融资 账户类型：1 融资账户  2非融资账户
    const SHOP_USER_ACCOUNT_RONG = 1;//融资
    const SHOP_USER_ACCOUNT_NOT_RONG = 2;//非融资

    //account_type是否风控：1  风控  2 不风控
    const SHOP_USER_ENABLE_RISK = 1;//开启风控
    const SHOP_USER_DISABLE_RISK = 2;//关闭风控

    //refund_check退款平台审核开启关闭 退款审核 1开启  2关闭
    const ORDER_REFUND_ADMIN_CHECK_ENABLE = 1;//开启
    const ORDER_REFUND_ADMIN_CHECK_DISABLE = 2;//关闭

    //is_guarantee是否开启担保合同签署  0关闭1开启
    const SHOP_USER_IS_GUARANTEE_OPEN = 1;//开启
    const SHOP_USER_IS_GUARANTEE_CLOSE = 0;//关闭

    //contract_automatic合同自动充值   0关闭  1开启
    const SHOP_USER_CONTRACT_AUTOMATIC_OPEN = 1;//开启
    const SHOP_USER_CONTRACT_AUTOMATIC_CLOSE = 0;//关闭


    //sms_automatic短信自动充值   0关闭  1开启
    const SHOP_USER_SMS_AUTOMATIC_OPEN = 1;//开启
    const SHOP_USER_SMS_AUTOMATIC_CLOSE = 0;//关闭

    //report_bill线下报单融资功能  1开启 2关闭
    const SHOP_USER_REPORT_BILL_OPEN = 1;//开启
    const SHOP_USER_REPORT_BILL_CLOSE = 2;//关闭

    //is_automatic_report_bill是否开启自动报单同步  1开启 2关闭
    const SHOP_USER_IS_AUTOMATIC_REPORT_BILL_OPEN = 1;//开启
    const SHOP_USER_IS_AUTOMATIC_REPORT_BILL_CLOSE = 2;//关闭

    //下单必填紧急联系人  1开启 2关闭
    const SHOP_USER_EMERGENCY_CONTACT_OPEN = 1;//开启
    const SHOP_USER_EMERGENCY_CONTACT_CLOSE = 2;//关闭

    const FAPTYPE_LIST = [
        '1' => '普通发票',
        '2' => '增值税发票'
    ];

    const ACCOUNT_TYPE_LIST = [
        '1' => '融资账户',
        '2' => '非融资账户'
    ];
    const SHOP_RISK_LIST = [
        1 => '风控',
        2 => '不风控'
    ];


    //获取入驻类型
    public static function getType($type)
    {
        $type_name = array(1 => '公司', 2 => '个人'); //入驻类型 1、公司 2、个人
        return getValue($type, $type_name);
    }

    //获取账户类型
    public static function getAccountName($account)
    {
        $account_name = array(1 => '融资账户', 2 => '非融资账户'); //账户类型：1 融资账户  2非融资账户
        return getValue($account, $account_name);
    }

    //是否风控
    public static function isAccountType($account_type)
    {
        $account_type_name = array(1 => '风控', 2 => '不风控'); //是否风控：1  风控  2 不风控
        return getValue($account_type, $account_type_name);
    }

    //获取发票类型
    public static function getFaptypeName($faptype)
    {
        $faptype_name = array('1' => '普通发票', '2' => '增值税发票', '1,2' => '普通发票+增值税发票'); //发票类型：1普通发票  2增值税发票  1,2普通发票+增值税发票;
        return getValue($faptype, $faptype_name);
    }

    //获取签署合同类型
    public static function getContractTypeName($contract_type)
    {
        $contract_type_name = array(1 => '蚂蚁智能合同', 2 => 'E签宝合同'); //合同类型：1蚂蚁智能合同  2E签宝合同
        return getValue($contract_type, $contract_type_name);
    }

    //获取签署方式
    public static function getSignNumName($sign_num)
    {
        $sign_num_name = array(1 => '自营两方', 2 => '供应商两方', 3 => '三方'); //签署方数量：1自营两方  2供应商两方  3三方
        return getValue($sign_num, $sign_num_name);
    }

    //是否开启担保合同签署
    public static function isGuarantee($is_guarantee)
    {
        $is_guarantee_name = array(0 => '关闭', 1 => '开启'); //是否开启担保合同签署 0关闭1开启
        return getValue($is_guarantee, $is_guarantee_name);
    }

    //是否允许互相担保
    public static function isMutualGuarantee($mutual_guarantee)
    {
        $mutual_guarantee_name = array(0 => '禁止', 1 => '允许'); //是否允许互相担保  0禁止  1允许
        return getValue($mutual_guarantee, $mutual_guarantee_name);
    }

    //获取供应商审核状态
    public static function getShopUserStatus($status)
    {
        $shop_user_status = array(1 => '已审核', 2 => '未审核', 3 => '拒绝'); //状态：1已审核 2未审核 3拒绝
        return getValue($status, $shop_user_status);
    }

    //是否启用
    public static function isEndisable($endisable)
    {
        $endisable_name = array(1 => '启用', 2 => '禁用'); //是否启用：1 启用  2禁用
        return getValue($endisable, $endisable_name);
    }

    public static function isOpenOrClose($is_open_or_close)
    {
        $is_open_or_close_name = array(1 => '开启', 2 => '关闭'); //1开启 2关闭
        return getValue($is_open_or_close, $is_open_or_close_name);
    }

    //查询店铺信息
    protected function GetShopUser($where)
    {
        $data = $this
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //供应商列表
    protected function GetSupplierList($pageSize, $where)
    {
        $select = array('su.id', 'su.shopname', 'su.photo', 'su.people', 'su.contact_tel', 'su.category_id', 'su.faptype', 'su.status', 'su.endisable', 'c.name as category_name', 'oa.mobile as account_num');
        $data = $this
            ->from('sp_shop_user_2 as su')
            ->leftjoin('sp_category as c', 'c.id', 'su.category_id')
            ->leftJoin('sp_other_auth_group as oag', function ($join) {
                $join->where('oag.rules', '=', '*')
                    ->where('oag.type', '=', '2')
                    ->where('oag.user_id', '=', '0');
            })
            ->leftJoin('sp_other_admin as oa', function ($join) {
                $join->on('oa.user_id', '=', 'su.member_id')
                    ->on('oa.role_id', '=', 'oag.id')
                    ->where('oa.type', '=', '2')
                    ->where('oa.is_delete', '=', '0');
            })
            ->select($select)
            ->where($where)
            ->orderBy('su.id', 'desc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //根据id编辑供应商信息
    protected function editShopUserById($id, $data)
    {
        $where = array(
            'id' => $id,
        );
        return $data = $this->where($where)->update($data);
    }

    //添加
    protected function addData($data)
    {
        return $data = $this->insert($data);
    }

    //供应商审核列表
    protected function GetSupplierCheckList($pageSize, $where, $status_where)
    {
        $select = array('su.id', 'su.member_id', 'su.shopname', 'su.photo', 'su.people', 'su.contact_tel', 'su.category_id', 'su.faptype', 'su.status', 'su.addtime', 'c.name', 'oa.mobile as account_num');
        $data = $this
            ->from('sp_shop_user_2 as su')
            ->leftjoin('sp_category as c', 'c.id', 'su.category_id')
            ->leftJoin('sp_other_auth_group as oag', function ($join) {
                $join->where('oag.rules', '=', '*')
                    ->where('oag.type', '=', '2')
                    ->where('oag.user_id', '=', '0');
            })
            ->leftJoin('sp_other_admin as oa', function ($join) {
                $join->on('oa.user_id', '=', 'su.member_id')
                    ->on('oa.role_id', '=', 'oag.id')
                    ->where('oa.type', '=', '2')
                    ->where('oa.is_delete', '=', '0');
            })
            ->select($select)
            ->where($where)
            ->where($status_where)
            ->orderBy('su.id', 'desc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //供应商审核状态数量
    protected function GetSupplierStatusCount($where, $status_where)
    {
        $data = $this
            ->from('sp_shop_user_2 as su')
            ->leftjoin('sp_category as c', 'c.id', 'su.category_id')
            ->leftJoin('sp_other_auth_group as oag', function ($join) {
                $join->where('oag.rules', '=', '*')
                    ->where('oag.type', '=', '2')
                    ->where('oag.user_id', '=', '0');
            })
            ->leftJoin('sp_other_admin as oa', function ($join) {
                $join->on('oa.user_id', '=', 'su.member_id')
                    ->on('oa.role_id', '=', 'oag.id')
                    ->where('oa.type', '=', '2')
                    ->where('oa.is_delete', '=', '0');
            })
            ->where($where)
            ->where($status_where)
            ->count();
        return $data;
    }

    //供应商列表-订单选择修改
    protected function getShopUsers()
    {
        $select = array('id', 'shopname', 'member_id');
        $data = $this->select($select)
            ->orderBy('id', 'desc')
            ->get();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

}
